<?php
/**
 * Created by PhpStorm.
 * User: HeiYanHeiMao
 * Date: 2020/4/28
 * Time: 16:10
 */

namespace app\controller;


use app\middleware\Check;
use app\model\UserModel;
use think\facade\Validate;
use think\Request;
use think\response\Json;
use think\validate\ValidateRule;


class User
{
    protected $middleware = [Check::class];

    /**列表
     * @param Request $request
     * @return Json
     */
    public function list(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['limit'] = $request->post('limit');//每页显示数量
        $input['page'] = $request->post('page');//当前页
        $input['school_name'] = $request->post('school_name');
        $input['status'] = $request->post('status');//用户状态
        $input['type'] = $request->post('type');//类型
        // 参数校验
        $validate = Validate::rule([
            'limit' => ValidateRule::isRequire(null, '缺少参数'),
            'page' => ValidateRule::isRequire(null, '缺少参数'),
            'school_name' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'status' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } elseif (in_array($value, ['', '0', '1', '2', '3'], true)) {
                    return true;
                } else {
                    return '非法参数';
                }
            },
            'type' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } elseif (in_array($value, ['', '1', '2'], true)) {
                    return true;
                } else {
                    return '非法参数';
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return UserModel::list($input, $request->post('token'));
    }


    /**审核
     * @param Request $request
     * @return Json
     */
    public function changeStatus(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['status'] = $request->post('status');
        $input['remark'] = $request->post('remark');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'remark' => function ($value) {
                return $value === null ? '缺少参数' : true;
            },
            'status' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } elseif (in_array($value, ['2', '3'], true)) {
                    return true;
                } else {
                    return '非法参数';
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return UserModel::changeStatus($input, $request->post('token'));
    }

    /**系统接单状态修改
     * @param Request $request
     * @return Json
     */
    public function orderTakingChange(Request $request)
    {
        if (!request()->isPost()) return jsonFail('请采用正确的方式登录!');
        $input['id'] = $request->post('id');
        $input['is_system_order_taking'] = $request->post('is_system_order_taking');
        // 参数校验
        $validate = Validate::rule([
            'id' => ValidateRule::isRequire(null, '缺少参数'),
            'is_system_order_taking' => function ($value) {
                if ($value === null) {
                    return '缺少参数';
                } elseif (in_array($value, ['0', '1'], true)) {
                    return true;
                } else {
                    return '非法参数';
                }
            }
        ]);
        if (!$validate->check($input)) {
            return jsonFail($validate->getError());
        }
        return UserModel::orderTakingChange($input, $request->post('token'));
    }
}